package net.jeeshop.web.action.front.bbs;

import net.jeeshop.core.Services;
import net.jeeshop.services.front.account.bean.Account;
import net.jeeshop.services.front.bbs.BbsCommentService;
import net.jeeshop.services.front.bbs.BbsDetailService;
import net.jeeshop.services.front.bbs.bean.BbsComment;
import net.jeeshop.services.front.bbs.bean.BbsDetail;
import net.jeeshop.web.action.front.FrontBaseController;
import net.jeeshop.web.util.LoginUserHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zuohao on 16/7/7.
 */
@Controller("frontBbsCommentAction")
@RequestMapping("bbsComment")
public class BbsCommentAction extends FrontBaseController<BbsComment> {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BbsCommentAction.class);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private BbsCommentService bbsCommentService;

    @Override
    public Services<BbsComment> getService() {
        return bbsCommentService;
    }

    /**
     * 评论或点赞，type=1点赞，type=2评论，type=3回复
     *
     * @param bbsDetailId
     * @param content
     * @param toUserId
     * @param type
     * @return
     */
    @RequestMapping("insertBbsComment")
    @ResponseBody
    public Map<String, Object> insertBbsComment(@RequestParam("bbsDetailId") String bbsDetailId,
                                                @RequestParam(value = "content", required = false) String content,
                                                @RequestParam(value = "toUserId", required = false) String toUserId,
                                                @RequestParam("type") String type, @RequestParam("userId") String userId) {
        Map<String, Object> result = new HashMap<String, Object>();

        String createId = userId;
        //判断是否给自己评论
        if (type.equals("2") && createId.equals(toUserId)) {
            result.put("code", "000012");
            result.put("msg", "不能给自己评论");
            return result;
        }
        //type=1为点赞
        if (type.equals("1")) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("createId", createId);
            map.put("bbsDetailId", bbsDetailId);
            map.put("type", "1");
            List<BbsComment> bbsCommentList = bbsCommentService.selectBbsCommentByType(map);
            if (bbsCommentList.size() > 0) {
                //已点赞
                result.put("code", "000013");
                result.put("msg", "已点赞");
                return result;
            }
        }
        BbsComment bbsComment = new BbsComment();
        bbsComment.setBbsDetailId(bbsDetailId);
        bbsComment.setContent(content);
        bbsComment.setCreateId(createId);
        bbsComment.setToUserId(toUserId);
        bbsComment.setType(type);
        bbsComment.setCreateTime(sdf.format(System.currentTimeMillis()));
        bbsComment.setStatus("y");
        bbsCommentService.insertBbsComment(bbsComment);
        result.put("code", "000000");
        result.put("msg", "OK");
        return result;
    }

    /**
     * 根据发表的论坛Id查找评论列表
     *
     * @param bbsDetailId
     * @return
     */
    @RequestMapping("selectBbsCommentByBbsDetailId")
    @ResponseBody
    public Map selectBbsCommentByBbsDetailId(@RequestParam("bbsDetailId") String bbsDetailId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bbsDetailId", bbsDetailId);
//        map.put("type", 1);
//        List<BbsComment> bbsZanList=bbsCommentService.selectBbsCommentByBbsDetailId(map);
        map.put("type", 2);
        List<BbsComment> bbsCommentList = bbsCommentService.selectBbsCommentByBbsDetailId(map);
        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("bbsZanList",bbsZanList);
        result.put("bbsCommentList", bbsCommentList);
        return result;
    }

    @RequestMapping("deleteBbsCommentById")
    @ResponseBody
    public String deleteBbsCommentById(@RequestParam("bbsCommentId") String bbsCommentId) {
        Account acc = LoginUserHolder.getLoginAccount();
        if (acc == null || StringUtils.isBlank(acc.getAccount())) {
            return "请先登录";
        }
        //用户只能操作当前用户的数据
        String createId = acc.getId();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("createId", createId);
        map.put("id", bbsCommentId);
        bbsCommentService.deleteBbsCommentById(map);
        return "true";
    }
}
